id_array = []
finput = open("input.txt", "r")
while !finput.eof do
  id_array << finput.readline.chomp
  #puts finput.readline
end
finput.close

ids = id_array.join(",")

sql_template = "(SELECT %s AS ChannelID, MerchantID, count(1) AS Total FROM C%sMerchantBidProduct WHERE MerchantID IN (%s) GROUP BY MerchantID ORDER BY MerchantID ASC)"

finput = open("channel.txt", "r")
sqls = []
sqls << "SELECT MerchantID FROM Merchant WHERE MerchantID IN (3505,4057,5156,6523,14014,17321,45322,56952,58083,58446,61835,61893,73870,75943,83182,83240,83253,83254,83255,83261,83262,83263,83264,83265,83272,83273,83274,83275,83276,83277,83278,83279,83280,83295,83296,83297,83298,83299,83300,83304,83305,83306,83307,83308,83309,83310,83311,83312,83313,83318,83319,83320,83321,83323,83324,83325,83326,83327,83328,83329,83330,83331,83333,83334,83339,83341,83343,83344,83345,83346,83347,83363,83365,83366,83367,83368,83369,83371,83372,83373,83374,83375,83376,83377,83378,83379,83380,83381,83382,83383,83384,83385,83386,83389,83390,83392,83393,83394,83396,83397,83398,83399,83401,83402,83404,83406,83407,83408,83409,83411,83412,83413,83414,83415,83416,83417,83418,83420,83421,83422,83423,83424,83426,83427) "
while !finput.eof do
  ch = finput.readline.chomp
  sqls << sql_template % [ch, ch, ids]
end
finput.close

open("merchants_result.sql", "w") { |f| f.write(sqls.join("\r\n UNION \r\n"))}